package com.turing.post.organization.impl;

import java.util.Date;

import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;

import com.turing.post.common.util.PCPSCommonDao;
import com.turing.post.common.util.SysOperLog;
import com.turing.post.common.util.SysOperLogDao;
import com.turing.post.organization.util.BusinessDistrict;
import com.turing.post.organization.util.BusinessDistrictBusiness;
import com.turing.post.organization.util.BusinessDistrictDao;
import com.turing.post.organization.util.OrganApplicationException;
import com.turing.post.organization.util.OrganRespCode;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;
import com.turing.post.util.PCPSWebInput;

/**
 * 商圈业务接口
 * 
 * @author zxp
 * @Dec 20, 2012
 */
@Stateless(mappedName = "organization.impl.BusinessDistrictBusinessImpl")
@Remote
@Local
public class BusinessDistrictBusinessImpl implements BusinessDistrictBusiness {

	@EJB(mappedName = "organization.impl.BusinessDistrictDaoImpl")
	private BusinessDistrictDao businessDistrictDaoImpl;

	@EJB(mappedName = "	common.impl.PCPSCommonDaoImpl")
	private PCPSCommonDao commonDaoImpl;

	@EJB(mappedName = "common.impl.SysOperLogDaoImpl")
	private SysOperLogDao sysOperLogDaoImpl;

	@EJB(mappedName = "common.impl.PCPSCommonDaoImpl")
	private PCPSCommonDao pcpsCommonDaoImpl;

	@Override
	public void insertBusinessDistrictInfo(PCPSWebInput webInput,
			BusinessDistrict businessDistrict) throws PCPSApplicationException {
		// 登录用户机构号为空
		if (PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
			throw new OrganApplicationException(
					OrganApplicationException.ERRCODE_CATEGORY_APPLICATION,
					OrganRespCode.ISSUE_INSERT_UPDATE, "登录用户机构号为NULL");
		}
		// 业务检查
		if (businessDistrict == null) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL,
					"BusinessDistrict");
		}
		// 名称
		if (PCPSUtil.isEmpty(businessDistrict.getBusinessDistrictName())) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL,
					"BusinessDistrictName");
		}

		// 平台机构管理员归属机构号为空
		if (!PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
			String issueCompanyNo = PCPSUtil.getIssueCompanyNo(webInput);
			// 归属发卡商
			businessDistrict.setIssueCompanyNo(issueCompanyNo);
		} else {
			throw new OrganApplicationException(
					OrganApplicationException.ERRCODE_CATEGORY_APPLICATION,
					OrganRespCode.ISSUE_INSERT_UPDATE, "超级管理员没有归属发卡商，禁止添加商圈!");
		}
		// 获取商圈主键
		businessDistrict.setBusinessDistrictId(commonDaoImpl
				.getUniqueIntegerKey());
		if (businessDistrictDaoImpl.IsBusinessDistrictName(businessDistrict
				.getIssueCompanyNo(), businessDistrict
				.getBusinessDistrictName(), null) > 0) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL,
					businessDistrict.getBusinessDistrictName() + ":商圈名称已经存在");
		}
		// 插入商圈
		businessDistrictDaoImpl.insertBusinessDistrict(businessDistrict);
		// 插入数据库操作日志
		insertRecordingOperationLog(webInput, "创建商圈", businessDistrict,
				SysOperLog.operType_Add, "issueCompanyNo");
	}

	@Override
	public void updateBusinessDistrictInfo(PCPSWebInput webInput,
			BusinessDistrict businessDistrict) throws PCPSApplicationException {
		// 登录用户机构号为空
		if (PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
			throw new OrganApplicationException(
					OrganApplicationException.ERRCODE_CATEGORY_APPLICATION,
					OrganRespCode.ISSUE_INSERT_UPDATE, "登录用户机构号为NULL");
		}
		// 查询
		BusinessDistrict _businessDistrict = businessDistrictDaoImpl
				.getBusinessDistrictByBusinessDistrictId(businessDistrict
						.getBusinessDistrictId());
		// 业务检查
		if (_businessDistrict == null) {
			throw new OrganApplicationException(
					OrganApplicationException.ERRCODE_CATEGORY_APPLICATION,
					OrganRespCode.ISSUE_INSERT_UPDATE, "待修改商圈数据库中不存在!");
		}
		_businessDistrict.setBusinessDistrictName(businessDistrict
				.getBusinessDistrictName());
		_businessDistrict.setSuperBusinessDistrictId(businessDistrict
				.getSuperBusinessDistrictId());
		if (businessDistrictDaoImpl.IsBusinessDistrictName(businessDistrict
				.getIssueCompanyNo(), businessDistrict
				.getBusinessDistrictName(), businessDistrict
				.getBusinessDistrictId()) > 0) {
			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL,
					businessDistrict.getBusinessDistrictName() + ":商圈名称已经存在");
		}
		// 修改商圈
		businessDistrictDaoImpl.updateBusinessDistrict(_businessDistrict);

		// 插入数据库操作日志
		insertRecordingOperationLog(webInput, "修改商圈", businessDistrict,
				SysOperLog.operType_Edit, "BusinessDistrictName:"
						+ businessDistrict.getBusinessDistrictName());
	}

	private void insertRecordingOperationLog(PCPSWebInput webInput,
			String logName, BusinessDistrict obj, String operType,
			String lastFieldName) throws PCPSApplicationException {
		String logRemark = PCPSUtil.RecordingOperationLog(obj, lastFieldName);
		if (!PCPSUtil.isEmpty(logRemark)) {
			// 插入数据库日志
			SysOperLog sysOperLog = new SysOperLog();
			if (webInput != null && webInput.getOperId() != null) {
				sysOperLog.setOperId(webInput.getOperId());
				sysOperLog.setOperType(operType);
				sysOperLog.setLogName(logName);
				if (webInput != null) {
					sysOperLog.setIpAddress(webInput.getIpAddress());
				}
				if (!PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
					sysOperLog.setIssueCompanyNo(webInput.getOperBranchNo()
							.substring(0, 6));
					sysOperLog.setBranchNo(webInput.getOperBranchNo());
				}
				sysOperLog.setRemark(logRemark);
				sysOperLogDaoImpl.insertSysOperLog(sysOperLog);
			}
		}
	}

	@Override
	public void removeBusinessDistrictInfo(PCPSWebInput webInput,
			BusinessDistrict businessDistrict) throws PCPSApplicationException {
		// 登录用户机构号为空
		if (PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
			throw new OrganApplicationException(
					OrganApplicationException.ERRCODE_CATEGORY_APPLICATION,
					OrganRespCode.ISSUE_INSERT_UPDATE, "登录用户机构号为NULL");
		}
		// 查询
		BusinessDistrict _businessDistrict = businessDistrictDaoImpl
				.getBusinessDistrictByBusinessDistrictId(businessDistrict
						.getBusinessDistrictId());
		// 业务检查
		if (_businessDistrict == null) {
			throw new OrganApplicationException(
					OrganApplicationException.ERRCODE_CATEGORY_APPLICATION,
					OrganRespCode.ISSUE_INSERT_UPDATE, "待删除商圈数据库中不存在!");
		}

		// 检查是否已经被引用
		if (businessDistrictDaoImpl
				.checkBusinessDistrictMerchant(_businessDistrict
						.getBusinessDistrictId()) > 0) {
			throw new OrganApplicationException(
					OrganApplicationException.ERRCODE_CATEGORY_APPLICATION,
					OrganRespCode.ISSUE_INSERT_UPDATE, "待删除商圈已经绑定了商户,请先解除绑定关系!");
		}
		if (businessDistrictDaoImpl
				.checkBusinessDistrictCardClassic(_businessDistrict
						.getBusinessDistrictId()) > 0) {
			throw new OrganApplicationException(
					OrganApplicationException.ERRCODE_CATEGORY_APPLICATION,
					OrganRespCode.ISSUE_INSERT_UPDATE,
					"待删除商圈已经绑定了卡种类,请先解除绑定关系!");
		}
		if (businessDistrictDaoImpl
				.checkBusinessDistrictChild(_businessDistrict
						.getBusinessDistrictId()) > 0) {
			throw new OrganApplicationException(
					OrganApplicationException.ERRCODE_CATEGORY_APPLICATION,
					OrganRespCode.ISSUE_INSERT_UPDATE, "待删除商圈存在二级商圈,请先删除二级商圈!");
		}
		// 删除商圈
		businessDistrictDaoImpl.deleteBusinessDistrict(_businessDistrict
				.getBusinessDistrictId());

		// 插入数据库操作日志
		insertRecordingOperationLog(webInput, "删除商圈", businessDistrict,
				SysOperLog.operType_Edit, "BusinessDistrictName:"
						+ businessDistrict.getBusinessDistrictName());
	}

	@Override
	public void authBusinessDistrict(PCPSWebInput webInput,
			BusinessDistrict businessDistrict) throws PCPSApplicationException {
		// 登录用户机构号为空
		if (PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
			throw new OrganApplicationException(
					OrganApplicationException.ERRCODE_CATEGORY_APPLICATION,
					OrganRespCode.ISSUE_INSERT_UPDATE, "登录用户机构号为NULL");
		}
		// 查询
		BusinessDistrict bd = businessDistrictDaoImpl
				.getBusinessDistrictByBusinessDistrictId(businessDistrict
						.getBusinessDistrictId());
		// 业务检查
		if (bd == null) {
			throw new OrganApplicationException(
					OrganApplicationException.ERRCODE_CATEGORY_APPLICATION,
					OrganRespCode.ISSUE_INSERT_UPDATE, "待审核商圈数据库中不存在!");
		}
		// 审核人与发起人不能相同
		if (webInput.getOperId().equals(businessDistrict.getCreateOperId())) {
			throw new OrganApplicationException(
					OrganApplicationException.ERRCODE_CATEGORY_APPLICATION,
					OrganRespCode.ISSUE_INSERT_UPDATE, "审核人与发起人不能相同!");
		}
		// 取当前时间
		Date occurTime = pcpsCommonDaoImpl.getDatabaseSysTime();
		// 1.新增审核
		if (BusinessDistrict.STATUS_INSERT.equals(businessDistrict.getStatus())) {
			bd.setStatus(businessDistrict.getStatus());
			bd.setAuthOperId(webInput.getOperId());
			bd.setAuthTime(occurTime);
			// 插入数据库操作日志
			insertRecordingOperationLog(webInput, "商圈新增审核通过", businessDistrict,
					SysOperLog.operType_Edit, "BusinessDistrictName:"
							+ businessDistrict.getBusinessDistrictName());
		}
		// 2.修改审核
		else if (BusinessDistrict.STATUS_UPDATE.equals(businessDistrict
				.getStatus())) {

		}
		// 3.删除审核
		else if (BusinessDistrict.STATUS_DELETE.equals(businessDistrict
				.getStatus())) {

		}
		// 更新商圈
		businessDistrictDaoImpl.updateBusinessDistrict(bd);
	}

	@Override
	public void unAuthBusinessDistrict(PCPSWebInput webInput,
			BusinessDistrict businessDistrict) throws PCPSApplicationException {
		// TODO Auto-generated method stub

	}
}
